package cn.tedu.service.customer.impl;

import cn.tedu.commons.exception.ServiceException;
import cn.tedu.commons.response.ServiceCode;
import cn.tedu.mapper.customer.CustomerInfMapper;
import cn.tedu.pojo.customer.dto.CustomerInfDTO;
import cn.tedu.pojo.customer.entity.CustomerInf;
import cn.tedu.pojo.customer.vo.CustomerInfVO;
import cn.tedu.service.customer.ICustomerInfService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CustomerInfServiceImpl implements ICustomerInfService {
    @Autowired
    private CustomerInfMapper customerInfMapper;


    @Override
    public void addNew(CustomerInfDTO customerInfDTO) {

        CustomerInf customerInf = new CustomerInf();
        customerInf.setUserPhone(customerInfDTO.getUserPhone());
        customerInf.setUserAge(customerInfDTO.getUserAge());
        customerInf.setUserEmail(customerInfDTO.getUserEmail());
        LocalDateTime now =LocalDateTime.now();
        customerInf.setGmtCreate(now);
        customerInf.setGmtModified(now);
        log.debug("即将增加信息");
        int rows = customerInfMapper.insert(customerInf);
        if (rows!=1){
            throw new ServiceException(ServiceCode.INTERNAL_SERVER_ERROR,"服务器繁忙，稍后再试");
        }

    }

    @Override
    public CustomerInf getInfById(Long userId) {
        CustomerInf inf = customerInfMapper.getInfById(userId);
        return inf;
    }

    @Override
    public CustomerInf updateDelete(Long userId, Integer isDelete) {
        CustomerInf inf= customerInfMapper.getInfById(userId);
        if (inf==null){
            throw new ServiceException(ServiceCode.NOT_FOUND,"该用户信息不存在");
        }
        customerInfMapper.updateDelete(userId,isDelete);
        if (isDelete==1){
            log.debug("用户为启用状态，{}",isDelete);
        }
        log.debug("用户为禁用状态，{}",isDelete);
        return null;
    }

    @Override
    public CustomerInfDTO updateInfById(CustomerInfDTO customerInfDTO) {
        CustomerInf oldInf = customerInfMapper.getInfById(customerInfDTO.getId());
        if (oldInf == null){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"该用户信息不存在");
        }
        CustomerInf customerInf = new CustomerInf();
        customerInf.setUserPhone(customerInfDTO.getUserPhone());
        customerInf.setUserEmail(customerInfDTO.getUserEmail());
        customerInf.setUserAge(customerInfDTO.getUserAge());
        customerInf.setGmtModified(LocalDateTime.now());
        customerInfMapper.updateInfById(customerInfDTO.getId());
        return customerInfDTO;
    }

    @Override
    public CustomerInf getAddrByDelete(Long userId, Integer isDelete) {
        CustomerInfVO inf = customerInfMapper.getInfByDelete(userId, isDelete);

        return null;
    }

    @Override
    public List<CustomerInfVO> list() {

        return customerInfMapper.list();
    }
}
